package edu.zju.gis.dldsj.server.controller;

import edu.zju.gis.dldsj.server.base.BaseController;
import edu.zju.gis.dldsj.server.common.Result;
import edu.zju.gis.dldsj.server.config.CommonSetting;
import edu.zju.gis.dldsj.server.constant.CodeConstants;
import edu.zju.gis.dldsj.server.entity.User;
import edu.zju.gis.dldsj.server.entity.search.UserSearch;
import edu.zju.gis.dldsj.server.service.UserService;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User, UserService, String, UserSearch> {
    private CommonSetting setting;

    /**
     * 用户登录
     *
     * @param input
     * @param session
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(@RequestBody Map<String, Object> input, HttpSession session) throws Exception {
        String name = String.valueOf(input.get("name"));
        String pass = String.valueOf(input.get("password"));
        User user = this.service.select(name, pass);
        if (user == null) {
            return Result.error("unvalid user name or password").setCode(CodeConstants.USERNAME_OR_PASSWORD_ERROR);
        } else {
            session.setAttribute("userId", user.getId());
            session.setAttribute("userName", user.getName());
            return new Result<User>().setBody(user).setCode(200);
        }
    }

    /**
     * 用户登出/注销
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public Result logout(HttpSession session) {
        try {
            session.removeAttribute("userId");
            session.removeAttribute("userName");
            return Result.success().setMessage("logout success");
        } catch (RuntimeException e) {
            return Result.error("logout error").setCode(CodeConstants.SERVICE_ERROR);
        }
    }

    /**
     * 用户登陆状态
     *
     * @param session
     * @return userId
     */
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @ResponseBody
    public Result getloginstatus(HttpSession session) {
        Object userId = session.getAttribute("userId");
        if (userId != null) {
            User user = service.select(String.valueOf(userId));
            return Result.success().setBody(user);
        }
        return Result.error("has not login").setCode(CodeConstants.USER_NOT_EXIST);
    }

    /**
     * 测试，从session中读取id数据
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public Result test(HttpSession session) {
        Object userId = session.getAttribute("userId");
        if (userId != null) {
            User user = service.select(String.valueOf(userId));
            return Result.success().setBody(user);
        }
        return Result.error("has not log in").setCode(CodeConstants.USER_NOT_EXIST);
    }
}
